home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 10.5 KB | 460 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: PRAlert.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWODTYPS_H
- #include "FWODTyps.h"
- #endif
-
- #ifndef PRALERT_H
- #include "PRAlert.h"
- #endif
-
- #ifndef FWRESOUR_H
- #include "FWResour.h"
- #endif
-
- #ifndef FWSTRING_H
- #include "FWString.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_Errors_defined
- #include <ErrorDef.xh>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
- #include <TextUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__ICONS__)
- #include <Icons.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__DIALOGS__)
- #include <Dialogs.h>
- #endif
-
- //========================================================================================
- // Runtime Informations
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwtoolbx
- #endif
-
- //========================================================================================
- // Macintosh Dialog Refcon structure
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- struct FW_SMacAlertRefCon
- {
- short fOkItemID;
- short fCancelItemID;
- };
-
- #define kDialogID 500
- // ----- dialog item ids
- #define kButton1ID 1
- #define kButton2ID 2
- #define kButton3ID 3
- #define kMessageID 4
- #define kIconID 5
-
- #endif
-
- #ifdef FW_BUILD_MAC
- //========================================================================================
- // MacAlertDialogFilter
- //========================================================================================
-
- static pascal Boolean MacAlertDialogFilter(DialogPtr theDialog,
- EventRecord * theEvent,
- short *itemHit)
- {
- Boolean result = FALSE;
-
- FW_SMacAlertRefCon* alertRefCon = (FW_SMacAlertRefCon*)::GetWRefCon(theDialog);
-
- switch (theEvent->what)
- {
- case keyDown:
- case autoKey:
- {
- switch (theEvent->message & charCodeMask)
- {
- case '.':
- if (theEvent->modifiers & cmdKey && alertRefCon->fCancelItemID != 0)
- {
- result = TRUE;
- *itemHit = alertRefCon->fCancelItemID;
- }
- break;
-
- case 0x0D:
- case 0x03:
- result = TRUE;
- *itemHit = alertRefCon->fOkItemID;
- break;
-
- case 0x1B:
- if (alertRefCon->fCancelItemID != 0)
- {
- result = TRUE;
- *itemHit = alertRefCon->fCancelItemID;
- }
- break;
- }
-
- if (result) // flash the button
- {
- long theTick;
- short itemType;
- Handle hItem;
- Rect box;
-
- ::GetDialogItem(theDialog, *itemHit, &itemType, &hItem, &box);
- ::HiliteControl((ControlHandle)hItem, 1);
- ::Delay(6,&theTick);
- ::HiliteControl((ControlHandle)hItem, 0);
- }
- }
- break;
-
- case updateEvt:
- if (theDialog == (DialogPtr)theEvent->message)
- {
- PenState ps;
- Rect box;
- Handle handle;
- short type;
-
- ::SetPort(theDialog);
- ::GetPenState(&ps);
- ::PenSize(3, 3);
- ::GetDialogItem(theDialog, alertRefCon->fOkItemID, &type, &handle, &box);
- ::InsetRect(&box, -4, -4);
- ::FrameRoundRect(&box, 16, 16);
- ::SetPenState(&ps);
- }
- break;
- }
-
- return result;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // MacSetButtonText
- //----------------------------------------------------------------------------------------
-
- static void MacSetButtonText(DialogPtr dialog, short buttonID, Str32 text, FW_Boolean moveButton)
- {
- Rect rect;
- short type;
- Handle handle;
-
- ::GetDialogItem(dialog, buttonID, &type, &handle, &rect);
- if (text[0] != 0)
- {
- ::SetControlTitle(ControlHandle(handle), text);
- if (moveButton)
- {
- ::OffsetRect(&rect, 10, 0);
- ::SetDialogItem(dialog, buttonID, type, handle, &rect);
- ::MoveControl(ControlHandle(handle), rect.left, rect.top);
- }
- }
- else
- {
- ::HideControl(ControlHandle(handle));
- }
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_PrivAlert
- //----------------------------------------------------------------------------------------
- static void MacGetStrings(Environment* ev, Str32 *butString, short index1, short index2, short index3)
- {
- FW_CAcquireCFMResourceAccess a(ev);
- if (index1 != 0)
- {
- ::GetIndString(butString[0], kDialogID, index1);
- FW_FailOnError(::ResError());
- }
- if (index2 != 0)
- {
- ::GetIndString(butString[1], kDialogID, index2);
- FW_FailOnError(::ResError());
- }
- if (index3 != 0)
- {
- ::GetIndString(butString[2], kDialogID, index3);
- FW_FailOnError(::ResError());
- }
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivAlert
- //----------------------------------------------------------------------------------------
- FW_AlertResult SL_API FW_PrivAlert(Environment* ev,
- FW_HString captionRep,
- FW_HString messageRep,
- FW_ButtonType buttonType,
- FW_IconType iconType,
- FW_DefaultButton defaultButton,
- FW_Boolean beep)
- {
- FW_CString caption(captionRep);
- FW_CString message(messageRep);
-
- #ifdef FW_BUILD_WIN
- UINT dialogFlags = buttonType | iconType | defaultButton | MB_TASKMODAL;
- dialogFlags ^= dialogFlags & MB_SYSTEMMODAL;
-
- // ::MessageBox() doesn't work if mouse capturing is active,
- // so we release the capture (if any) and then restore it
-
- const HWND hwndCapture = ::GetCapture();
-
- if (hwndCapture != NULL)
- ::ReleaseCapture();
-
- if (beep)
- ::MessageBeep((UINT)iconType);
-
- FW_CAcquireNulTerminatedString255 szMessage(message);
- FW_CAcquireNulTerminatedString255 szCaption(caption);
-
- short result = ::MessageBox(NULL, szMessage, szCaption, dialogFlags);
-
- // Set mouse capture to the window that had it
- if (hwndCapture != 0)
- ::SetCapture(hwndCapture);
-
- return result;
- #endif
-
- #ifdef FW_BUILD_MAC
-
- GrafPtr curPort;
- ::GetPort(&curPort);
-
- DialogPtr dlg = NULL;
- FW_VOLATILE(dlg);
-
- short items[3];
- short item;
-
- FW_TRY
- {
- // ----- Open create the dialog (can't do exception here because used to display exception messages)
- {
- FW_CAcquireCFMResourceAccess a(ev);
- dlg = ::GetNewDialog(kDialogID, NULL, WindowPtr(-1));
- FW_FailOnError(::ResError());
- }
-
- FW_SMacAlertRefCon alertRefCon;
- ::SetWRefCon(dlg, (long)&alertRefCon);
-
- Rect rect;
- short type;
- Handle handle;
-
- ::GetDialogItem(dlg, kMessageID, &type, &handle, &rect);
-
- Str255 str;
- message.ExportPascal(str);
- ::SetDialogItemText(handle, str);
-
- Str32 butString[3];
- butString[0][0] = 0;
- butString[1][0] = 0;
- butString[2][0] = 0;
-
- FW_Boolean moveButton = FALSE;
-
- switch (buttonType)
- {
- case FW_kOK:
- {
- MacGetStrings(ev, butString, 1, 0, 0);
- alertRefCon.fOkItemID = 1;
- alertRefCon.fCancelItemID = 0;
- items[0] = FW_kOKButtonPressed;
- break;
- }
- case FW_kOKCancel:
- {
- MacGetStrings(ev, butString, 1, 2, 0);
- if (defaultButton == FW_kDefaultButton2)
- {
- alertRefCon.fCancelItemID = 0;
- alertRefCon.fOkItemID = 2;
- }
- else
- {
- alertRefCon.fOkItemID = 1;
- alertRefCon.fCancelItemID = 2;
- }
- items[0] = FW_kOKButtonPressed;
- items[1] = FW_kCancelButtonPressed;
- break;
- }
- case FW_kAbortRetryIgnore:
- {
- MacGetStrings(ev, butString, 3, 4, 5);
- alertRefCon.fOkItemID = defaultButton + 1;
- alertRefCon.fCancelItemID = 0;
- items[0] = FW_kAbortButtonPressed;
- items[1] = FW_kRetryButtonPressed;
- items[2] = FW_kIgnoreButtonPressed;
- break;
- }
- case FW_kYesNoCancel:
- {
- MacGetStrings(ev, butString, 6, 7, 2);
- if (defaultButton == FW_kDefaultButton1)
- {
- alertRefCon.fOkItemID = 1;
- alertRefCon.fCancelItemID = 3;
- }
- else if (defaultButton == FW_kDefaultButton2)
- {
- alertRefCon.fOkItemID = 2;
- alertRefCon.fCancelItemID = 3;
- }
- else
- {
- // I can't decide if Esc means Yes or No
- alertRefCon.fOkItemID = 3;
- alertRefCon.fCancelItemID = 0;
- }
- items[0] = FW_kYesButtonPressed;
- items[1] = FW_kNoButtonPressed;
- items[2] = FW_kCancelButtonPressed;
- moveButton = TRUE;
- break;
- }
- case FW_kYesNo:
- {
- MacGetStrings(ev, butString, 6, 7, 0);
- alertRefCon.fCancelItemID = 0;
- if (defaultButton == FW_kDefaultButton2)
- alertRefCon.fOkItemID = 2;
- else
- alertRefCon.fOkItemID = 1;
- items[0] = FW_kYesButtonPressed;
- items[1] = FW_kNoButtonPressed;
- break;
- }
- case FW_kRetryCancel:
- {
- MacGetStrings(ev, butString, 4, 2, 0);
- if (defaultButton == FW_kDefaultButton2)
- {
- alertRefCon.fCancelItemID = 0;
- alertRefCon.fOkItemID = 2;
- }
- else
- {
- alertRefCon.fOkItemID = 1;
- alertRefCon.fCancelItemID = 2;
- }
- items[0] = FW_kRetryButtonPressed;
- items[1] = FW_kCancelButtonPressed;
- break;
- }
- default:
- FW_ASSERT(0); // unknown dialogFlags
- break;
- }
-
- MacSetButtonText(dlg, kButton1ID, butString[0], FALSE);
- MacSetButtonText(dlg, kButton2ID, butString[1], moveButton);
- MacSetButtonText(dlg, kButton3ID, butString[2], FALSE);
-
- // ----- Icons
- Handle hIcon = NULL;
- switch (iconType)
- {
- case FW_kStopAlert:
- hIcon = ::GetIcon(stopIcon);
- break;
-
- case FW_kCautionAlert:
- hIcon = ::GetIcon(cautionIcon);
- break;
-
- case FW_kNoteAlert:
- hIcon = ::GetIcon(noteIcon);
- break;
- };
-
- ::GetDialogItem(dlg, kIconID, &type, &handle, &rect);
- if (hIcon)
- {
- ::SetDialogItem(dlg, kIconID, type, hIcon, &rect);
- }
- else
- {
- ::GetDialogItem(dlg, kIconID, &type, &handle, &rect);
- short left = rect.left;
- ::OffsetRect(&rect, 1000, 0);
- ::SetDialogItem(dlg, kIconID, type, hIcon, &rect);
-
- ::GetDialogItem(dlg, kMessageID, &type, &handle, &rect);
- rect.left = left;
- ::SetDialogItem(dlg, kMessageID, type, handle, &rect);
- }
-
- if (beep)
- ::SysBeep(16);
-
- ::ShowWindow(dlg);
-
- // ----- Nothing can fail from here on -----
- ModalFilterUPP filterProc = NewModalFilterProc(&MacAlertDialogFilter);
- do
- {
- ::ModalDialog(filterProc, &item);
- } while (item <kButton1ID || item > kButton3ID);
-
- DisposeRoutineDescriptor(filterProc);
-
- }
- FW_CATCH_BEGIN
- FW_CATCH_REFERENCE(FW_XException, exception)
- {
- FW_SetException(ev, exception);
- }
- FW_CATCH_EVERYTHING ()
- {
- FW_SetEvError(ev, kODErrUndefined);
- }
- FW_CATCH_END
-
- if (dlg)
- ::DisposeDialog(dlg);
-
- ::SetPort(curPort);
-
- return (items[item - 1]);
- #endif
- }
-